package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.model.MallItemModel;
import com.zw.yzk.kabu.utils.CircleTransform;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/1/6.
 */

public class MallItemListAdapter extends RecyclerView.Adapter<MallItemListAdapter.Holder> {

    private List<MallItemModel> goodList;
    private Context context;


    public MallItemListAdapter(List<MallItemModel> list) {
        this.goodList = new ArrayList<>();
        if (list != null) {
            this.goodList.addAll(list);
        }
    }

    public void refresh(List<MallItemModel> list) {
        this.goodList.clear();
        if (list != null) {
            this.goodList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void add(List<MallItemModel> list) {
        if (list != null) {
            int start = goodList.size();
            this.goodList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_item_mall_item, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        MallItemModel model = goodList.get(position);

        Glide.with(context)
                .load(model.getHomeImge())
                .placeholder(R.drawable.ic_default_head)
                .error(R.drawable.ic_default_head)
                .transform(new CircleTransform(context))
                .into(holder.subSymbol);
        holder.subTitle.setText(model.getComtent());
        holder.setData(model.getItemList(), context);
    }

    @Override
    public int getItemCount() {
        return goodList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.sub_symbol)
        ImageView subSymbol;
        @Bind(R.id.sub_title)
        TextView subTitle;
        @Bind(R.id.child)
        RecyclerView child;

        private MallChildAdapter adapter;


        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(List<MallItemModel.Item> list, Context c) {
            if(adapter == null) {
                adapter = new MallChildAdapter(list);
                child.setLayoutManager(new LinearLayoutManager(c, LinearLayoutManager.VERTICAL, false));
                child.setAdapter(adapter);
            } else {
                adapter.refresh(list);
            }
        }
    }


}
